package fr.upvm.lem3.vm3d.ui.swing.menu;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;

import fr.upvm.lem3.vm3d.ui.swing.util.MyWindowsLookAndFeel;
import fr.upvm.lem3.vm3d.ui.swing.util.SwingUtil;
import fr.upvm.lem3.vm3d.ui.swing.volume3d.TexturePanel;

public class CommonToolBar extends JToolBar implements ActionListener{

	/**
	 */
	private static final long serialVersionUID = 1L;
	private JFrame parentFrame;
    private CommonToolBarController controller;
    private JButton btn_Clip; 		// use to make a clip into the 3d volume
    private JButton btn_Slice; 		// use to make a slice into the 3d volume
    
    MyMouseAdpter mouseAdapter;
    /*
     * use to construct a toolBar object
     */
	public CommonToolBar( JFrame frame){
		 super(frame.getTitle()+" - Common");
		 this.setToolTipText("Common Toolbar");
		 this.setFloatable(false);
		 this.setParentFrame(frame);
		 this.mouseAdapter = new MyMouseAdpter();
		 this.setBorder(BorderFactory.createEmptyBorder());
		 this.setCommonToolBarController(new CommonToolBarController(this));
		 initComponents();
		 this.setVisible(false);
	 }
	/**
	 * initialize the menuBar
	 */
	private void initComponents(){
		this.setRollover(true);
		this.addSeparator(new Dimension(5,5));	
		this.add(getBtn_Clip());
		this.addSeparator(new Dimension(10,10));
		this.add(getBtn_Slice());
		this.addSeparator(new Dimension(10,10));
//		this.add(getPanelTexture());
//		this.addSeparator(new Dimension(10,10));
		
	}
//	/**
//	 * 
//	 * @return
//	 */
//	 private TexturePanel getPanelTexture() {
//		TexturePanel panel = getCommonToolBarController().getPanelTexture();
//		return panel;
//		
//	}
	/**
	  * handle the actions with happen on click on these 2 buttons.
	  */
	public void actionPerformed(ActionEvent e) {
		//TODO complete this methode
		String cmd= e.getActionCommand();	
		if(SLICE.equals(cmd)){
		   getCommonToolBarController();
		   }else{
			   if(CLIP.equals(cmd)){
				   //
//				   getCommonToolBarController();
//				   MyWindowsLookAndFeel.setNativeLookAndFeel(getParentFrame());
			   }
		   }
	}
	/**
	 * @param toolBarController the toolBarController to set
	 */
	public void setCommonToolBarController(CommonToolBarController controller) {
		this.controller = controller;
	}

	/**
	 * @return the toolBarController
	 */
	public CommonToolBarController getCommonToolBarController() {
		return controller;
	}
	/**
	 * @param parentFrame the parentFrame to set
	 */
	public void setParentFrame(JFrame parentFrame) {
		this.parentFrame = parentFrame;
	}

	/**
	 * @return the parentFrame
	 */
	public JFrame getParentFrame() {
		return parentFrame;
	}
	
	static final private String TOOLTIP_CLIP="Get a clip";
	static final private String CLIP_LABEL = "Clip";
	static final private String CLIP = "Clip";
	/**
	 * @return the btn_Clip
	 */
	public JButton getBtn_Clip() {
		if(btn_Clip== null){
			btn_Clip= new JButton();
			btn_Clip.setFont(new Font("Dialog", Font.TRUETYPE_FONT, 15));
			btn_Clip.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
//			btn_Clip.setAlignmentX(Component.CENTER_ALIGNMENT);
			btn_Clip.setVerticalTextPosition(SwingConstants.CENTER);
			btn_Clip.setHorizontalTextPosition(SwingConstants.CENTER);
			btn_Clip.setFocusable(false);
			btn_Clip.setText(CLIP_LABEL);
			btn_Clip.setToolTipText(TOOLTIP_CLIP);
			btn_Clip.setOpaque(false);
			btn_Clip.addActionListener(this);
			btn_Clip.setActionCommand(CLIP);
			btn_Clip.setMnemonic(KeyEvent.VK_C);
			btn_Clip.addMouseListener(mouseAdapter);
			
		}
		return btn_Clip;
	}
	/**
	 * @param btn_Clip the btn_Clip to set
	 */
	public void setBtn_Clip(JButton btn_Clip) {
		this.btn_Clip = btn_Clip;
	}
	
	static final private String TOOLTIP_SLICE="Get a slice";
	static final private String SLICE_LABEL = "Slice";
	static final private String SLICE = "Slice";
	/**
	 * @return the btn_Slice
	 */
	public JButton getBtn_Slice() {
		if(btn_Slice== null){
			btn_Slice= new JButton();
			btn_Slice.setFont(new Font("Dialog", Font.TRUETYPE_FONT, 15));
			btn_Slice.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
			btn_Slice.setAlignmentX(Component.CENTER_ALIGNMENT);
			btn_Slice.setFocusable(false);
			btn_Slice.setText(SLICE_LABEL);
			btn_Slice.setToolTipText(TOOLTIP_SLICE);
			btn_Slice.setOpaque(false);
			btn_Slice.addActionListener(this);
			btn_Slice.setActionCommand(SLICE);
			btn_Slice.setMnemonic(KeyEvent.VK_S);
			btn_Slice.addMouseListener( mouseAdapter);
			
		}
		return btn_Slice;
	}
	/**
	 * @param btn_Slice the btn_Slice to set
	 */
	public void setBtn_Slice(JButton btn_Slice) {
		this.btn_Slice = btn_Slice;
	}
	
	/**
	 * Internal adapter class
	 *******************************************************************
	 */
	class MyMouseAdpter extends MouseAdapter{
		
		public MyMouseAdpter() {}
		
		@Override
	    public void mouseEntered(MouseEvent e) {
			((JButton)e.getSource()).setBorder(BorderFactory.createLineBorder(Color.lightGray, 1, true));
		}

	    @Override
	    public void mouseExited(MouseEvent e) {
	    	((JButton)e.getSource()).setBorder(BorderFactory. createEmptyBorder(1,1,1,1));
	    }
	}
}

